<script setup>
/* 新版本更新弹出框 */
const appUpdateChannel = 'app:updater'
import {ipc, Process} from '@/utils/ipcRenderer'
import {ipcApiRoute} from "@/api/main"
import {useGlobalConf} from '@/utils/useGlobalConf'
import {useLocale} from '@/utils/useLocale'
const {globalConf} = useGlobalConf()
const $updaterI18n = useLocale('updater')
const messageToastr = useMessage()
const myDialog = useDialog()
//订阅更新消息
const showProcessModal = ref(false)
const processState = ref('')
const percentNumber = ref(0)
const nextVersion = ref('')
let myDialogInstance
const latestVersionDialog = (version) => {
  myDialogInstance = myDialog.info({
    title: () => $updaterI18n.value.appUpdate,
    content: () => $updaterI18n.value.publishMessage.replace('{{version}}', version),
    positiveText: () => $updaterI18n.value.yes,
    negativeText: () => $updaterI18n.value.no,
    onPositiveClick: () => {
      myDialogInstance.loading = true
      nextVersion.value = version
      ipc.send(ipcApiRoute['home.downloadLatestVersion'])
      return false
    }
  })
}
const onAppUpdateHandler = (event, args) => {
  args = JSON.parse(args)
  const senderName = args.senderName
  myDialogInstance?.destroy()
  myDialogInstance && (myDialogInstance = void 0)
  switch (args.status) {
    case -1: //error
      if (senderName === 'sys') {//系统强制或启动软件时更新的情况，Error不处理
        return
      }
      messageToastr.error(() => (args.desc.message || $updaterI18n.value.errorMessage))
      break
    case 1:  //available
      if (senderName === 'sys') {//系统强制或启动软件时更新的情况，Available不处理
        return
      }
      //新版发布提醒
      latestVersionDialog(args.version)
      break
    case 2:  //noAvailable
      if (senderName === 'sys') {//系统强制或启动软件时更新的情况，NoAvailable不处理
        return
      }
      messageToastr.info(() => $updaterI18n.value.latestVersion)
      break
    case 3:  //downloading
      if (!showProcessModal.value) {
        showProcessModal.value = true
      }
      percentNumber.value = args.percentNumber
      processState.value = `${$updaterI18n.value.downloaded}${args.percentNumber}%(${args.transferredSize}/${args.totalSize})`
      break
    case 4:  //downloaded
      showProcessModal.value = false
      messageToastr.success(() => $updaterI18n.value.downloadCompleted)
      break
  }
}
ipc.off(appUpdateChannel, onAppUpdateHandler)
ipc.on(appUpdateChannel, onAppUpdateHandler)
//有新版发布时提醒我
if (!globalConf._systemUpdaterForce && globalConf.appAutoUpdates === 'sendnotify' && Process.env.EE_WINDOW_KEY === 'main') {
  ipc.send(ipcApiRoute['home.checkForUpdate'], {senderName: 'main'})
}
</script>

<template>
  <n-modal
      v-model:show="showProcessModal"
      :mask-closable="false"
      :close-on-esc="false"
      :closable="false"
  >
    <n-card
        style="width:520px"
        :title="$updaterI18n.downloadProgress.replace('{{version}}', nextVersion)"
        :bordered="true"
        size="huge"
        role="dialog"
        aria-modal="true"
    >
      <n-progress
          type="line"
          :percentage="percentNumber"
          :indicator-placement="'inside'"
          processing
      />
      <br/>
      <span>{{ processState }}</span>
    </n-card>
  </n-modal>
</template>

<style scoped lang="less">

</style>